import React from 'react'
import { connect } from 'react-redux'
import { Collapse, Divider, Icon, Popconfirm, Typography } from 'antd'
import { calculateScore, getCurrentOrgProgramsBasicQuizzes } from '../../../action/program'

const {Panel} = Collapse
const {Title} = Typography

class BasicQuizzesManagementBody extends React.Component {

  componentDidMount = () => {
    this.props.getCurrentOrgProgramsBasicQuizzes()
  }

  getData = () => {
    const {programsBasicQuizAssignments} = this.props
    return programsBasicQuizAssignments.map(item => {
      return <Panel header={item.program.title} key={item.program.title}>
        {this.getAssignments(item)}
      </Panel>
    })
  }

  getAssignments = item => {
    const assignments = []
    item.tasks.forEach(task => {
      task.assignments.filter(assignment => assignment.type === 'BASIC_QUIZ')
        .forEach(assignment => {
          assignments.push({
            taskId: task.id,
            taskTitle: task.title,
            assignmentTitle: assignment.title,
            assignmentId: assignment.id,
          })
        })
    })

    return assignments.map(assignment => {
      return <p>{assignment.taskTitle} <Icon type="caret-right" /> {assignment.assignmentTitle} <Divider type='vertical'/>
        <Popconfirm
          title='确定重算?'
          onConfirm={()=>this.calculateScore(assignment.taskId,assignment.assignmentId)}
        >
        <a href='javascript:(0)'>重算</a>
        </Popconfirm>
      </p>
    })
  }

  calculateScore = (taskId, assignmentId) => {
    this.props.calculateScore(taskId,assignmentId)
  }
  render () {
    return <div style={{padding: 20}}>
      <Title level={4}>当前组织下所有客观题 </Title>
      <Collapse bordered={false} defaultActiveKey={['1']}>
        {this.getData()}
      </Collapse>
    </div>
  }
}

const mapStateToProps = ({programsBasicQuizAssignments}) => ({
  programsBasicQuizAssignments
})

const mapDispatchToProps = dispatch => ({
  getCurrentOrgProgramsBasicQuizzes: () => dispatch(getCurrentOrgProgramsBasicQuizzes()),
  calculateScore : (taskId, assignmentId) => dispatch(calculateScore(taskId,assignmentId))
})

export default connect(mapStateToProps, mapDispatchToProps)(BasicQuizzesManagementBody)
